package com.tchhy.provider.repository

import com.google.gson.Gson
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import java.io.File

/**
 * @author: admin
 * @date: 2019/8/20
 * @des:
 *
 **/
open class BaseRepository {

    fun getRequestJsonBody(obj: Any): RequestBody {
        return Gson().toJson(obj)
            .toRequestBody("application/json; charset=utf-8".toMediaTypeOrNull())
//        return RequestBody.create("application/json; charset=utf-8".toMediaTypeOrNull(), Gson().toJson(obj))
    }

    fun getRequestMutiBody(file: File): RequestBody {
        return file.asRequestBody("multipart/form-data".toMediaTypeOrNull())
//        return RequestBody.create("multipart/form-data".toMediaTypeOrNull(), file)
    }

    fun getMutiPart(file: File): MultipartBody.Part {
        return MultipartBody.Part.createFormData("file", file.name, getRequestMutiBody(file))
    }

    fun getMutiPart(byteArray: ByteArray): MultipartBody.Part {
        return MultipartBody.Part.createFormData(
            "file",
            System.currentTimeMillis().toString() + "_profile.png",
            getRequestMutiBodys(byteArray)
        )
    }

    fun getRequestMutiBodys(byteArray: ByteArray): RequestBody {
        return byteArray.toRequestBody("multipart/form-data".toMediaTypeOrNull())
//        return RequestBody.create("multipart/form-data".toMediaTypeOrNull(), byteArray)
    }


}